﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.IO;
using System.Printing;
using Newtonsoft.Json;

namespace BizBrowser.RPT
{
    public class PrintUtil
    {
        static string baseDir = Path.Combine(Application.StartupPath, "reports/ReportPrints.ini");

        

        [DllImport("kernel32.dll")]
        private static extern int GetPrivateProfileString(string section, string key, string defVal, StringBuilder retVal, int size, string filePath);

        [DllImport("kernel32.dll")] 
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);


        public static string GetValue(string key)
        {
            StringBuilder s = new StringBuilder(1024);
            GetPrivateProfileString("CONFIG", key, "", s, 1024, baseDir);
            return s.ToString();
        }

        public static void SetValue(string key, string value)
        {
            try
            {
                WritePrivateProfileString("CONFIG", key, value, baseDir);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



        /// <summary>
        /// 获取本地所有打印机列表
        /// </summary>
        /// <returns></returns>
        public static List<String> GetPrintList()
        {
            List<String> list = new List<string>();
            LocalPrintServer printServer = new LocalPrintServer();
            PrintQueueCollection coll = printServer.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local} );
            foreach (PrintQueue printer in coll)
            {
                list.Add(printer.Name);
            }
            return list;
        }

        public static String getJsonPrintList()
        {
            return JsonConvert.SerializeObject(GetPrintList());   
        }

    }
}
